<template>
  <div class="consult-body">
    <div class="consult-content-body">
      <div class="consult-article">
        <el-scrollbar style="width: 100%;height: 100%">
          <div class="consult-ul">
            <div v-for="(item,index) in dataList" :key="index">
              <div v-if="item.type === 0" class="consult-li">
                <el-image :src="image" style="width: 40px;height: 40px;float: left" />
                <div class="content-body">
                  <p v-if="item.content">{{ item.content }}</p>
                  <div class="content-left-image">
                    <el-image :src="consultLeft" />
                  </div>
                  <div v-if="undefined !==item.list && item.list.length > 0" class="custom-other-object">
                    <div class="question-title">相关问题：</div>
                    <div v-for="(obj,ind) in item.list" :key="index+'-c-'+ind" class="question-title-li" @click="queryEntity(obj)">{{ obj }}</div>
                  </div>
                  <div v-if="item.state === 201 " class="custom-other-button">
                    <el-button type="primary" size="mini">去留言板提问</el-button>
                  </div>
                </div>
                <div style="clear: both" />
              </div>
              <div v-if="item.type === 1" class="consult-li">
                <el-image :src="userImage" style="width: 40px;height: 40px;float: right" />
                <div class="content-body-right">
                  <p>{{ item.content }}</p>
                  <div class="content-right-image">
                    <el-image :src="consultRight" />
                  </div>
                </div>
                <div style="clear: both" />
              </div>
            </div>
            <div style="clear: both" />
          </div>
        </el-scrollbar>
      </div>
    </div>
    <div class="consult-content-right">
      <div class="consult-article">
        <consult-list @call-back="callBack" />
      </div>
    </div>
    <div class="consult-content-button">
      <div class="consult-article">
        <el-autocomplete
          v-model="blurry"
          style="width: 100%"
          :maxlength="50"
          show-word-limit
          placeholder="请在此处输入您的问题"
          :fetch-suggestions="querySearchAsync"
          @keyup.enter.native="sendMsg"
          @select="handleSelect"
        >
          <el-button slot="append" type="primary" :loading="loading" @click="sendMsg">发&nbsp;&nbsp;送</el-button>
        </el-autocomplete>
      </div>
    </div>
  </div>
</template>
<script>
import consultImage from '@/assets/images/consult.png'
import userConsult from '@/assets/images/user_consult.png'
import consultLft from '@/assets/images/consult-lft.png'
import consultRit from '@/assets/images/consult-rght.png'
import { queryAllSearch, queryAll } from '@/api/consult/user'
import ConsultList from './component/list'
export default {
  name: 'ConsultClientIndex',
  components: { ConsultList },
  data() {
    return {
      image: consultImage,
      userImage: userConsult,
      consultLeft: consultLft,
      consultRight: consultRit,
      loading: false,
      dataList: [
        { title: '', content: '您好，欢迎使用自助咨询服务，请随便问喔！问题示例：“宿舍”、“报销”...', list: [], id: 0, type: 0, state: 200 }
      ],
      blurry: ''
    }
  },
  methods: {
    callBack(data) {
      this.blurry = data
      this.sendMsg()
    },
    queryEntity(data) {
      this.blurry = data
      this.sendMsg()
    },
    querySearchAsync(queryString, cb) {
      queryString = queryString.replace(/\s*/g, '')
      if (queryString !== '') {
        queryAllSearch({ blurry: queryString }).then(res => {
          const { data } = res
          if (data !== null && data.length > 0) {
            const newData = data.map(item => {
              const back = { value: item.title }
              return back
            })
            cb(newData)
          } else {
            cb([])
          }
        }).catch(() => {
          cb([])
        })
      } else {
        cb([])
      }
    },
    handleSelect(item) {
      this.blurry = item.value
      this.sendMsg()
    },
    sendMsg() {
      this.blurry = this.blurry.replace(/\s*/g, '')
      if (this.blurry === '') {
        this.$message.warning('请输入要查询的内容')
      } else {
        const object = { title: '', content: this.blurry, list: [], type: 1, state: 200 }
        this.dataList.push(object)
        const qu = this.blurry
        this.blurry = ''
        queryAll({ blurry: qu }).then(res => {
          const { data, state, msg } = res
          if (state === 200) {
            const list = data.map(item => {
              return item.title
            })
            console.log(list)
            const back = { title: '', content: '', list: list, type: 0, state: 300 }
            this.dataList.push(back)
          } else if (state === 205) {
            if (undefined !== data.glwt && data.glwt !== null) {
              const back = { title: '', content: data.nr, list: data.glwt.split(','), type: 0, state: 200 }
              this.dataList.push(back)
            } else {
              const back = { title: '', content: data.nr, list: [], type: 0, state: 200 }
              this.dataList.push(back)
            }
          } else if (state === 201) {
            const back = { title: '', content: msg, list: [], type: 0, state: 201 }
            this.dataList.push(back)
          }
        }).catch(() => {
        })
      }
    }
  }
}
</script>

<style scoped>
.consult-body {
  width: 100%;
  height: 100%;
  position: relative;
  background-color: #f8f8f8;
  border: 1px solid #e6e6e6;
  display: contents;
}
/*当屏幕宽度大于815像素的时候*/
@media screen and (min-width: 815px){
  .consult-content-body {
    position: absolute;
    top: 0px;
    left: 0px;
    right: 250px;
    bottom: 60px;
    z-index: 1;
    overflow: hidden;
    padding: 10px 15px;
    box-sizing: border-box;
    background-color: #f8f8f8;
  }
  .consult-content-right {
    position: absolute;
    top:0px;
    right: 0px;
    bottom: 0px;
    width: 250px;
    z-index: 10;
    overflow: hidden;
    padding: 10px 15px;
    box-sizing: border-box;
    background-color: #ffffff;
    border-left: 1px solid #e6e6e6;
    border-top: 1px solid #e6e6e6;
  }
  .consult-content-button {
    position: absolute;
    height: 60px;
    right: 250px;
    left: 0px;
    bottom: 0px;
    z-index: 10;
    overflow: hidden;
    padding: 10px 15px;
    box-sizing: border-box;
    background-color: #ffffff;
    border-top: 1px solid #e6e6e6;
  }
}
/*当屏幕宽度小于815像素的时候*/
@media screen and (max-width: 815px){
  .consult-content-body {
    position: absolute;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 90px;
    z-index: 1;
    overflow: hidden;
    padding: 10px 15px;
    box-sizing: border-box;
  }
  .consult-content-button {
    position: absolute;
    height: 90px;
    right: 0px;
    left: 0px;
    bottom: 0px;
    z-index: 10;
    overflow: hidden;
    padding: 10px 15px;
    box-sizing: border-box;
    background-color: #ffffff;
    border-top: 1px solid #e6e6e6;
  }
  .consult-content-right {
    display: none;
  }
}
.consult-article {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
}
.consult-ul {
  width: 100%;
  height: 100%;
}
.consult-li {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  position: relative;
  margin-top: 15px;
}
.content-body {
  float: left;
  max-width: calc(100% - 60px);
  margin-left: 20px;
  line-height: 20px;
  color: #454545;
  font-size: 13px;
  text-align: left;
  border: 1px solid #e6e6e6;
  border-radius: 6px;
  position: relative;
  background-color: #ffffff;
  padding: 5px 10px;
  box-sizing: border-box;
}
.content-body-right{
  float: right;
  max-width: calc(100% - 60px);
  margin-right: 20px;
  line-height: 20px;
  color: #ffffff;
  font-size: 13px;
  border-radius: 6px;
  border: 1px solid #e6e6e6;
  position: relative;
  padding: 5px 10px;
  box-sizing: border-box;
  background-color: #399df0;
}
.content-left-image {
  position: absolute;
  width: 14px;
  height: 14px;
  top: 7px;
  left: -9px;
}
.content-right-image {
  position: absolute;
  width: 14px;
  height: 14px;
  top: 7px;
  right: -13px;
}
.question-title {
  width: 100%;
  height: 20px;
  line-height: 20px;
  color: #8f8f8f;
}
.question-title-li {
  width: 100%;
  height: 20px;
  line-height: 20px;
  text-decoration: underline;
  text-indent: 8px;
  position: relative;
  cursor: pointer;
}
.question-title-li::before{
  position: absolute;
  content: " ";
  width: 4px;
  height: 4px;
  top:8px;
  left: 0px;
  background-color: #454545;
  border-radius: 2px;
}
.custom-other-button {
  width: 100%;
  height: 28px;
  text-align: right;
}
.el-scrollbar >>> .el-scrollbar__wrap {
  overflow-x: hidden;
  overflow-y: auto;
  margin-right: 0px !important;
}
</style>
